﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dao;

namespace csharp_dao
{
    class Class1
    {
        /// <summary>
        /// dao 연결
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static dao.Database getDAODB(String filePath, String pwd="")
        {
            dao.DBEngine DAODBEngine = new dao.DBEngineClass();
            
            FileInfo fi = new FileInfo(filePath);
            dao.Database DAODataBase;
            if (fi.Exists)
            {
                DAODataBase = DAODBEngine.OpenDatabase(filePath, true, false, @"MS Access;PWD=" + pwd);
            }else{
                DAODataBase = DAODBEngine.CreateDatabase(filePath, dao.LanguageConstants.dbLangKorean, System.Reflection.Missing.Value);
            }

            return DAODataBase;
        }

        /// <summary>
        /// 테이블명 취득
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static List<String> fnGetDAOTables1(dao.Database db)
        {
            List<String> ret = new List<String>();
            
            foreach (dao.TableDef tbd in db.TableDefs)
            {
                if (tbd.Name.ToLower().Substring(0, 4) != @"msys")
                {
                    ret.Add(tbd.Name);
                }
            }

            return ret;
        }

        /// <summary>
        /// 테이블명 취득
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static List<String> fnGetDAOTables2(dao.Database db)
        {
            List<String> ret = new List<String>();
            dao.Recordset rs = db.ListTables();

            rs.MoveFirst();
            while (!rs.EOF)
            {
                if (rs.Fields["Name"].Value.ToString().ToLower().Substring(0, 4) != @"msys")
                {
                    ret.Add(rs.Fields["Name"].Value.ToString());
                }
                
                rs.MoveNext();
            }

            return ret;
        }

        /// <summary>
        /// 컬럼명 취득
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tbn"></param>
        /// <returns></returns>
        public static List<String> fnGetDAOFields1(dao.Database db, String tbn = @"Table1")
        {
            List<String> ret = new List<String>();
            TableDef td = db.TableDefs[tbn];

            foreach (dao.Field fld in td.Fields)
            {
                ret.Add(fld.Name);
            }

            return ret;
        }

        public static List<String> fnGetDAOFields2(dao.Database db, String tbn = @"Table1")
        {
            List<String> ret = new List<String>();
            Recordset rs = db.ListFields(tbn);

            rs.MoveFirst();
            while (!rs.EOF)
            {
                ret.Add(rs.Fields["Name"].Value.ToString());
                rs.MoveNext();
            }

            return ret;
        }
    }
}
